package controller;

import dao.PersonDao;
import dao.impl.PersonDaoImpl;
import entity.Person;
import service.PersonService;
import service.impl.PersonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/personController")
public class PersonController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(true);
        String op = req.getParameter("op");
        if ("members".equals(op)) {
            PersonService pe = new PersonServiceImpl();
            List<Person> me = null;
            try {
                me= pe.cx();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            req.setAttribute("me",me);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }

       else  if ("sb".equals(op)){
            int id= Integer.parseInt(req.getParameter("id"));
            PersonDao dao=new PersonDaoImpl();
            PrintWriter out=resp.getWriter();
            int a=0;
            try {
                a=dao.show(id);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (a>0){
                req.getRequestDispatcher("/personController?op=members").forward(req,resp);
            }
        }
        else if ("add".equals(op)) {
            // 新增添加人员逻辑
            Person person = new Person();
            person.setName(req.getParameter("name"));
            person.setSex(req.getParameter("gender"));
            person.setAge(Integer.parseInt(req.getParameter("age")));
            person.setRank(req.getParameter("level"));
            person.setDepartment(req.getParameter("department"));
            
            PersonService service = new PersonServiceImpl();
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            
            try {
                int result = service.insert(person);
                if (result > 0) {
                    out.print("{\"status\":\"success\",\"message\":\"添加成功\"}");

                } else {
                    out.print("{\"status\":\"error\",\"message\":\"添加失败\"}");
                }
            } catch (Exception e) {
                e.printStackTrace();
                out.print("{\"status\":\"error\",\"message\":\"系统错误: " + e.getMessage() + "\"}");
            }
            out.flush();
            out.close();
        }
    }
}
